//
//  AppDelegate.swift
//  OnemeetingDemo
//
//  Created by zhangheng on 2019/11/15.
//  Copyright © 2019 www.onemeeting.1meetingIos. All rights reserved.
//

import Cocoa

//@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!
    
    var toRelaunch:Bool = false

    //网页回调参数保存
    weak var urlDelegate: WebSchemaDelegate?
    
    lazy var applicationName:String = {
        if let bundleName = Bundle.main.object(forInfoDictionaryKey:"CFBundleName") {
            if let bundleNameAsString = bundleName as? String {
                return bundleNameAsString
            }
            else {
                NSLog("CFBundleName not a String!")
            }
        }
        else {
            NSLog("CFBundleName nil!")
        }
        return NSLocalizedString("壹会通", comment:"The name of this application")
    }()
    

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        //self.checkPermiss()
        
        self.populateMainMenu()
        
        
        self.load_UserInfoManager()
        
        self.windowController.showWindow(self)
        
        ZoomsdkManger.shared.sdkAuth(false)
        
        NetVersion.versionCompUpdate()
      
    }
    
    func checkScreen() {
        let isok = Permissions.canRecord()
        if isok == false {
            Permissions.showScreenRecordingPrompt()
        }
    }
    
    lazy var windowController: MainWindowController = {
        let main = MainWindowController()
        return main
    }()
    
    func load_UserInfoManager() {
        UserInfoManager.shared.lang = UserInfoManager.shared.lang ?? "zh-Hans"
        UserInfoManager.shared.disname =  UserInfoManager.shared.disname ?? "壹会通"
        UserInfoManager.shared.roomId = UserInfoManager.shared.roomId ?? "000000"
        UserInfoManager.shared.userId = UserInfoManager.shared.userId ?? "000000"
        UserInfoManager.shared.onAudio = UserInfoManager.shared.onAudio ?? false
        UserInfoManager.shared.onVideo = UserInfoManager.shared.onVideo ?? true
        UserInfoManager.shared.isOnLive = UserInfoManager.shared.onAudio ?? false
        
    }
    
    //网页回调
    func application(_ application: NSApplication, open urls: [URL]) {
        let url = urls[0]
        if(url.scheme == mt_api_join) || (url.scheme == mt_api_host){
            let resultDic = WebURL.getSchemeToDic(url)
            net_delay(time: 1) {
                if(self.urlDelegate != nil){
                    let type =  (url.scheme == mt_api_join) ? RtcType.join:RtcType.host
                    self.urlDelegate!.onHandleUrl(params:resultDic , type: type)
                }
            }
        }
    }
    
    //授权
    func checkPermiss() {
        
        //let accessibilityEnabled = Permissions.canAccessibility()
        //if accessibilityEnabled == true {
            //Permissions.openSetting_accessibility()
        //}
        
        let iscanRecord = Permissions.canRecord()
        if iscanRecord == true {
            Permissions.showScreenRecordingPrompt()
        }
        
    }
    
    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        NSApp.activate(ignoringOtherApps: false)
        TrtcEngine.tapDuck()
        return true
    }
    
    /*
    func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
        return true //YES-窗口程序两者都关闭，NO-只关闭窗口；
    }
    */

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
        if (self.toRelaunch) {
            NSWorkspace.shared.launchApplication(withBundleIdentifier: "com.1meeting.yihuitong", options: [NSWorkspace.LaunchOptions.async,  NSWorkspace.LaunchOptions.newInstance], additionalEventParamDescriptor: nil, launchIdentifier: nil)
        }
    }

    @objc func openWebSite(_ sender:Any?) {
        if let myURL = URL(string:"http://www.1meeting.cn/") {
            NSWorkspace.shared.open(myURL)
        }
    }
    
    @objc func openLicense(_ sender:Any?) {
        
    }
    
    func relaunchApp() {
        
        toRelaunch = true
        NSApplication.shared.terminate(true)
    }
}

class ZH: NSObject {
    class func delegate() -> AppDelegate? {
        return (NSApplication.shared.delegate as? AppDelegate)
    }
}

